package mka.puzzle.shape;

import java.awt.Graphics;
import java.util.ArrayList;

/***
 * Square
 * 
 * @version 20131227
 * 
 * @author NguyenBB
 */
public class Square extends Shape {
	/**
	 * leftUpPoint.
	 */
	private Point leftTopPoint;

	/**
	 * side.
	 */
	private double side;

	/**
	 * Constructor(Square()).
	 */
	public Square() {
		this.leftTopPoint = new Point(0, 0);
		this.side = 0;
	}

	/**
	 * Constructor(Square(Point upLeft)).
	 */
	public Square(Point upLeft) {
		this.leftTopPoint = new Point(upLeft.getX(), upLeft.getY());
		this.side = 0;
	}

	/**
	 * Constructor(Square(Point upLeft, double side)).
	 */
	public Square(Point upLeft, double side) {
		this.leftTopPoint = new Point(upLeft.getX(), upLeft.getY());
		this.side = side;
	}

	/**
	 * getLeftUpPoint.
	 * 
	 * @return Point
	 */
	public Point getLeftTopPoint() {
		return leftTopPoint;
	}

	/**
	 * setLeftUpPoint.
	 * 
	 * @param leftUpPoint
	 */
	public void setLeftTopPoint(Point leftTopPoint) {
		this.leftTopPoint = leftTopPoint;
	}

	/**
	 * getSide.
	 * 
	 * @return double
	 */
	public double getSide() {
		return side;
	}

	/**
	 * setSide.
	 * 
	 * @param side
	 */
	public void setSide(double side) {
		this.side = side;
	}

	/**
	 * getCenterPoint.
	 * 
	 * @return Point
	 */
	public Point getCenterPoint() {
		return new Point(Math.round(this.leftTopPoint.getX() + side / 2),
				Math.round(this.leftTopPoint.getY() + side / 2));
	}

	/***************************************************************
	 * Override from superclass.
	 ***************************************************************/

	/**
	 * getName.
	 * 
	 * @return String
	 */
	@Override
	public String getName() {
		return "Square";
	}

	/**
	 * getPerimeter.
	 * 
	 * @return double
	 */
	@Override
	public double getPerimeter() {
		return 4 * side;
	}

	/**
	 * getArea.
	 * 
	 * @return double
	 */
	@Override
	public double getArea() {
		return side * side;
	}

	/**
	 * allPointBound.
	 * 
	 * @return ArrayList<Point>
	 */
	@Override
	public ArrayList<Point> allPointBound() {
		ArrayList<Point> pointList = new ArrayList<Point>();
		// Min value of X
		double minX = this.getLeftTopPoint().getX();
		// Max value of X
		double maxX = this.getLeftTopPoint().getX() + this.side;
		// Min value of Y
		double minY = this.getLeftTopPoint().getY();
		// Max value of Y
		double maxY = this.getLeftTopPoint().getY() + this.side;

		for (double x = minX; x <= maxX; x++) {
			for (double y = minY; y <= maxY; y++) {
				if (x == minX)
					pointList.add(new Point(x, y));
				else if (x == maxX)
					pointList.add(new Point(x, y));
				else if (y == minY)
					pointList.add(new Point(x, y));
				else if (y == maxY)
					pointList.add(new Point(x, y));
			}
		}
		return pointList;
	}

	/**
	 * allPointShape.
	 * 
	 * @return ArrayList<Point>
	 */
	@Override
	public ArrayList<Point> allPointShape() {
		ArrayList<Point> pointList = new ArrayList<Point>();
		// Min value of X
		double minX = this.getLeftTopPoint().getX();
		// Max value of X
		double maxX = this.getLeftTopPoint().getX() + this.side;
		// Min value of Y
		double minY = this.getLeftTopPoint().getY();
		// Max value of Y
		double maxY = this.getLeftTopPoint().getY() + this.side;
		for (double x = minX; x <= maxX; x++) {
			for (double y = minY; y <= maxY; y++) {
				pointList.add(new Point(x, y));
			}
		}
		return pointList;
	}
	
	/***************************************************************
	 * Draw and paint method.
	 ***************************************************************/
	/***
	 * draw.
	 * 
	 * Draw the Square by java function
	 * 
	 * @param g Graphics
	 */
	public void draw(Graphics g) {
		g.drawRect((int) this.getLeftTopPoint().getX(), (int) this
				.getLeftTopPoint().getY(), (int) this.getSide(),
				(int) this.getSide());
	}
}
